
#项目需求：解析出所有城市名称  https://www.aqistudy.cn/historydata/

import requests
from lxml import etree
if __name__ == "__main__":
    #url = "https://www.aqistudy.cn/historydata/"
    #headers = {
    #    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/105.0.0.0 Safari/537.36'
    #}
    #page_text = requests.get(url=url,headers=headers).text
    #tree = etree.HTML(page_text)
    #host_li_list = tree.xpath('//div[@class="bottom"]/ul/li')
    #all_city_name = []
    #解析到热门城市的城市名称
    #for li in host_li_list:
    #    hot_city_name = li.xpath('./a/text()')[0]
    #    all_city_name.append(hot_city_name)
    #解析的是全部城市的名称
    #city_names_list = tree.xpath('//div[@class="bottom"]/ul/div[2]/li')
    #for li in city_names_list:
    #    city_name = li.xpath('./a/text()')[0]
    #    all_city_name.append(city_name)

    #print(all_city_name,len(all_city_name))

    url = "https://www.aqistudy.cn/historydata/"
    headers = {
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/105.0.0.0 Safari/537.36'
    }
    page_text = requests.get(url=url, headers=headers).text
    tree = etree.HTML(page_text)
    #tree.xpath('//div[@class="bottom"]/ul/li')
    #tree.xpath('//div[@class="bottom"]/ul/div[2]/li')
    a_list = tree.xpath('//div[@class="bottom"]/ul/li/a | //div[@class="bottom"]/ul/div[2]/li/a')
    all_city_name = []
    for a in a_list:
        city_name = a.xpath('./text()')[0]
        all_city_name.append(city_name)
    print(all_city_name,len(all_city_name))

